package com.tanhua.server.controller;

import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.server.service.MomentsService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("comments")
public class CommentController {

    @Autowired
    private MomentsService momentsService;

    /**
     * 评论列表
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> commentList(@RequestParam("movementId") String publishId,
                                                  @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                  @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = momentsService.findCommentList(publishId,page,pageSize);
            if(null != pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 发表评论
     * @param
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> sendComment(@RequestBody Map<String,String> param){
        try {
            String publishId = param.get("movementId");//动态ID
            String content = param.get("comment");//评论内容
            Boolean result = momentsService.sendComment(publishId,content);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 点赞评论
     * @param commentId 评论的ID
     * @return
     */
    @GetMapping("{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String commentId){
        try {
            Long like = momentsService.likeComment(commentId);
            if(null != like){
                return ResponseEntity.ok(like);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String commentId){
        try {
            Long disLike = momentsService.disLikeComment(commentId);
            if(null!=disLike){
                return ResponseEntity.ok(disLike);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
